import config from '../common/config.js';
import common from '../common/common.js';
import md5 from '../common/md5.js';

const httpReq = {};

const baseUrl = config.baseUrl;

const getVerificationCode = ''; //获取验证码
const verificationLogin = ''; //验证码登录--->没有直接注册
const automaticLogin = ''; //自动登录
const getPayPassVerCode = ''; //找回支付密码验证码
const findPayPassword = ''; //找回支付密码
const setPayPassword = ''; //设置支付密码
const updateUserMsg = ''; //更新用户信息
const exchangeMoney = ''; //兑换
const exchangeRecording = ''; //兑换记录
const gameList = ''; //游戏列表
const abountApp = ''; //关于应用

httpReq.getVerificationCode = function(reqData, cb) {
	reqBase(getVerificationCode, reqData, cb);
}

httpReq.verificationLogin = function(reqData, cb) {
	reqBase(verificationLogin, reqData, cb);
}

httpReq.automaticLogin = function(reqData, cb) {
	reqBase(automaticLogin, reqData, cb);
}

httpReq.getPayPassVerCode = function(reqData, cb) {
	reqBase(getPayPassVerCode, reqData, cb);
}

httpReq.findPayPassword = function(reqData, cb) {
	reqBase(findPayPassword, reqData, cb);
}

httpReq.setPayPassword = function(reqData, cb) {
	reqBase(setPayPassword, reqData, cb);
}

httpReq.updateUserMsg = function(reqData, cb) {
	reqBase(updateUserMsg, reqData, cb);
}

httpReq.exchangeMoney = function(reqData, cb) {
	reqBase(exchangeMoney, reqData, cb);
}

httpReq.exchangeRecording = function(reqData, cb) {
	reqBase(exchangeRecording, reqData, cb);
}

httpReq.gameList = function(reqData, cb) {
	reqBase(gameList, reqData, cb);
}

httpReq.abountApp = function(reqData, cb) {
	reqBase(abountApp, reqData, cb);
}

let _FormatASCII = function(obj) {
	let arr = [];
	for (let key in obj) {
		if (obj.hasOwnProperty(key)) {
			arr.push(key + '=' + obj[key]);
		}
	}
	arr.sort();
	return arr.join('&');
}

let _getParameter = function(obj) {
	obj.signCode = Date.parse(new Date()) / 1000;
	const signKey = 'ZXgZz*L8WiLW%8jXKbom0NmB9%UAaJMP';
	let signature = _FormatASCII(obj) + "&key=" + signKey;
	obj.sign = md5.md5(signature).toUpperCase();
	return obj;
}

let reqBase = function(url, reqData, cb, method) {
	reqData = _getParameter(reqData);
	reqData = _FormatASCII(reqData);
	uni.request({
		url: baseUrl + url,
		data: reqData,
		method: method || 'POST',
		header: {
			'Content-type': 'application/x-www-form-urlencoded;charset=utf-8'
		},
		success: (res) => {
			if (common.isTypeFunc(cb)) cb(res);
		}
	});
}

export default httpReq
